/**********************************************************************
 ** Copyright (C) 2009 Nokia Corporation.
 ** All rights reserved.
 **
 ** $LICENSE_TEXT$
 **********************************************************************/

#ifndef BLUETOOTHMANAGER_H
#define BLUETOOTHMANAGER_H

#include <QObject>
#include <QString>

class QDBusInterface;
class QDBusMessage;
class QIODevice;

class BtSingleDeviceSelector;
class BtSerialPortClient;

/**
 * @brief The BluetoothManager class manages steps necessary to provide a Bluetooth connection.
 *
 * This class initially tries to connect to a default device and, if no success, calls an interface to
 * select a new bluetooth device using a BtSingleDeviceSelector. After that, it configures a serial port connection using a BtSerialPortClient.
 * @see BtSerialPortClient, BtSingleDeviceSelector
 */
class BluetoothManager: public QObject
{
    Q_OBJECT
public:

    /**
     * Constructs a BluetoothManager object with the given parent.
     *
     * @param parent Parent.
     */
    explicit BluetoothManager(QObject *parent = 0);

    /**
     * BluetoothManager destructor
     */
    ~BluetoothManager();

private:

    /**
     * Setup QObject connections between this class, a BtSingleDeviceSelector and a BtSerialPortClient.
     */
    void setupConnections();

signals:

    /**
     * This signal is emitted after connect() has been called and a connection has been successfully established.
     */
    void connected();

    /**
     * This signal is emitted when the bluetooth device has been disconnected.
     */
    void disconnected();

    /**
     * This signal is emitted once every time new data is available from the device.
     * It will only be emitted again once new data is available.
     * @param data Data received from device.
     */
    void dataReceived(QByteArray data);

    /**
     * This signal is emitted after a device is defined and the serial connection has to be configured.
     */
    void deviceDefined(QString device);

public slots:

    /**
     * Connects to the default device. If it is unavailable, initiates a device selector dialog.
     */
    void connect();

    /**
     * Disconnects from the current device.
     */
    void disconnect();

    /**
     * Writes the content of data to the device.
     * @param data Data to be sent.
     */
    void sendData(QString data);

private slots:

    /**
     * Configures a serial connection on device with MAC serialDevice.
     * @param serialDevice
     */
    void setupSerialConnection(QString serialDevice);

    /**
     * Configures IODevice socket.
     * @param device
     */
    void setupIODevice(QIODevice* device);

    /**
     * Reads data from IODevice when available, process it, and emits the dataReceived(QString data) signal
     * with processed data as a QString.
     */
    void processNewData();

    /**
     * Process errors from bluetooth connection.
     */
    void processError();

    /**
     * Process disconnection of bluetooth device.
     */
    void processDisconnected();

private:
    BtSingleDeviceSelector *mp_deviceSelector;
    BtSerialPortClient *mp_serialClient;
    QIODevice *mp_ioDevice;

    bool m_isTryingDefault;
};

#endif // BLUETOOTHMANAGER_H
